﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using System.IO;


namespace Urbania.Utilities
{
    public class Loader
    {

        private static Dictionary<String, Texture2D> textures;
        private static Dictionary<String, SpriteFont> fonts;
        private static Dictionary<String, SoundEffect> sounds;
        private static Dictionary<string, string> scriptFilePaths;
        private static Dictionary<string, string> xmlFilePaths;
        private static ContentManager content;
        private static Texture2D defaultTex;
        private static string contentFilePath;

        public static string ContentFilePath { get { return contentFilePath; } }

        public static void InitTexLoader(GraphicsDevice graphicsDevice, ContentManager contentManager)
        {
            content = contentManager;
            defaultTex = new Texture2D(graphicsDevice, World.Tile.Width, World.Tile.Height, false, SurfaceFormat.Color);
            Color[] defaultData = new Color[World.Tile.Width * World.Tile.Height];
            for (int i = 0; i < defaultData.Length; i++) 
            {
                defaultData[i] = new Color(255, 255, 255, 255);
            }
            defaultTex.SetData(defaultData);
            textures = new Dictionary<string, Texture2D>();
            fonts = new Dictionary<string, SpriteFont>();
            sounds = new Dictionary<string, SoundEffect>();
            scriptFilePaths = new Dictionary<string, string>();
            xmlFilePaths = new Dictionary<string, string>();
            setDir();
        }

        public static void LoadScriptDir(string fileDir)
        {
            string[] dirs = Directory.GetFiles(contentFilePath + "\\" + fileDir);
            foreach (string s in dirs)
            {
                if (!scriptFilePaths.ContainsKey(s))
                {
                    string[] breakDown = s.Split('\\');
                    scriptFilePaths.Add(breakDown[breakDown.Length - 1], s);
                }
            }
        }

        public static Texture2D getDefaultTex()
        {
            return defaultTex;
        }

        public static void LoadXMLFile(string filePath)
        {
            if (xmlFilePaths.ContainsKey(filePath))
                return;
            else
                xmlFilePaths.Add(filePath, contentFilePath + filePath);
        }

        public static string GetXMLFile(string filePath)
        {
            if (xmlFilePaths.ContainsKey(filePath))
            {
                StreamReader reader = new StreamReader(xmlFilePaths[filePath]);
                return reader.ReadToEnd();
            }
            else
                return null;
        }

        public static void LoadScriptFile(string filePath)
        {
            if (scriptFilePaths.ContainsKey(filePath))
                return;
            else
                scriptFilePaths.Add(filePath, contentFilePath + filePath);
        }

        public static string GetScriptFile(string filePath)
        {
            if (scriptFilePaths.ContainsKey(filePath))
                return scriptFilePaths[filePath];
            else
                return null;
        }

        public static void LoadTex(String filename)
        {
            if (filename != "Tiles\\" && !textures.ContainsKey(filename))
            {
                Texture2D loaded = content.Load<Texture2D>(filename);
                textures.Add(filename, loaded);
            }
        }

        public static void LoadFont(String filename)
        {
            SpriteFont sFont = content.Load<SpriteFont>(filename);
            fonts.Add(filename, sFont);
        }

        public static void LoadSound(String filename)
        {
            SoundEffect sound = content.Load<SoundEffect>(filename);
            sounds.Add(filename, sound);
        }

        public static SpriteFont GetFont(String filename)
        {
            if (fonts.ContainsKey(filename))
                return fonts[filename];
            else
                return null;
        }

        public static SoundEffect GetSound(String filename)
        {
            if (sounds.ContainsKey(filename))
                return sounds[filename];
            else
                return null;
        }

        public static Texture2D GetTex(String filename)
        {
            if (textures.ContainsKey(filename))
            {
                return textures[filename];
            }
            else
            {
                return defaultTex;
            }
        }

        public static void setDir()
        {
            string dir = Directory.GetCurrentDirectory();
            string[] folder = dir.Split('\\');

            dir = "";

            for (int i = 0; i < folder.Length; i++)
            {
                if (i < folder.Length - 4)
                    dir += folder[i] + "\\";
            }
            dir += "UrbaniaContent\\";
            contentFilePath = dir;
        }

    }
}
